//
//  AnnounCementVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/16.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "AnnounCementVC.h"
#import "AnnounCementCell.h"
#import "myWkWebViewController.h"
#import "TableViewAnimationKit.h"


@interface AnnounCementVC ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *_data;
}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation AnnounCementVC



- (void)viewDidLayoutSubviews{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitle:@"公告通知" titleColor:[UIColor blackColor] font:font17];
    
    self.tableView.backgroundColor = self.view.backgroundColor;
    _data = @[];
    UIView *heder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 20)];
    heder.backgroundColor = self.view.backgroundColor;
    self.tableView.tableHeaderView = heder;
    
    
    [self loadingData];
    
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadingData];
        
    }];

    
}

- (void)loadingData{
    __weak typeof (self)weakself = self;
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Notice/platNotice" parameter:@{} successBlock:^(PPHttpRequest *request, id responseObj) {
        _data = responseObj;
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
        [TableViewAnimationKit toTopAnimationWithTableView:self.tableView];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [weakself.tableView.mj_header endRefreshing];
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];
}

- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-TabBarItemHeight-NavigationHeight+49) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"AnnounCementCell" bundle:nil] forCellReuseIdentifier:@"AnnounCementCell"];
        
        //UITableView去掉自带系统的分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _data[indexPath.row];
    AnnounCementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AnnounCementCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColorFromRGB(0xEBEBEB);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.smallLab.text = [NSString stringWithFormat:@"%@",[ToolClass strToTime:dic[@"add_time"] format:@"yyyy-MM-dd hh:mm:ss"]];
    cell.timeLab.text = [NSString stringWithFormat:@"%@",[ToolClass strToTime:dic[@"add_time"] format:@"yyyy-MM-dd hh:mm:ss"]];
    cell.content.text = [NSString stringWithFormat:@"%@",dic[@"introduction"]];
    cell.titleLba.text = [NSString stringWithFormat:@"%@",dic[@"title"]];
    cell.titleLba.textColor = UIColorFromRGB(0x1e9efa);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _data[indexPath.row];
    
    NSString *goodsname = dic[@"introduction"];
    CGSize titleSize = [goodsname boundingRectWithSize:CGSizeMake(SCREEN_W-70, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14] } context:nil].size;
    
    return titleSize.height + 80;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = _data[indexPath.row];
    [self loadUrl:dic[@"id"]];
}

- (void)loadUrl:(NSString *)noticID{
//    __weak typeof (self)weakself = self;
//    NSDictionary *dics = @{@"noticeId":noticID,@"flag":@"1"};
//    NSMutableDictionary *dic = @{@"noticeId":noticID,@"flag":@"1"}.mutableCopy;;
//    [dic setObject:[userDefaults objectForKey:@"admin_id"] forKey:@"admin_id"];
//    [dic setObject:[userDefaults objectForKey:@"guid"] forKey:@"guid"];
//    
//    [dic setObject:[ToolClass getData] forKey:@"time"];
//    NSString *token = [ToolClass gettoken:dic];
//    [dic setObject:token forKey:@"authKey"];

    myWkWebViewController *vc = [[myWkWebViewController alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/ShopApi/Html/itemDetail?noticeId=%@&flag=1",APP_HOST_ADDRESS,noticID];
    vc.myurl = url;
    vc.toTitle = @"公告详情";
    [self.navigationController pushViewController:vc animated:YES];
    
}

@end
